--[[Chunk
区块类
储存整个区块的数据
]]

Chunk = class()
Chunk.Height = 0

-- 创建一个区块
function Chunk:init(world,x,z)
	 self.world = world
    self.x = x
    self.z = z
    self.xPos = x * 16
    self.zPos = z * 16
    
    self.changed = true
    self.bindedRenderChunk = false
    self.readyToRender = false
end

-- 为区块创建空地图
function Chunk:createEmptyMap()
    self.blocks = {}
    for i = 1,Chunk.Height * 256 do
        self.blocks[i] = 0
    end
end

function Chunk:finishCreating()
    self.brightness = {}
    for i = 1,Chunk.Height * 256 do
        self.brightness[i] = 0
    end
    self.heightMap = {}
    for i = 1,256 do
        self.heightMap[i] = 0
    end
    self:updateAllHeightMap()
    self:updateAllVerticalBrightnessI()
end

function Chunk:genFlatTerrain(ys)
    local ch = Chunk.Height - 1
    for y = 0,ch do
        if ys[y] then
            for x = 0,15 do
                for z = 0,15 do
                    self.blocks[y * 256 + x * 16 + z + 1] = ys[y]
                end
            end
        end
    end
end

-- 更新区块的高度图,传入范围
function Chunk:calcHeightMap(x0,z0,x1,z1)
    for x = x0,x0 + x1 - 1 do
    	for z = z0,z0 + z1 - 1 do
    		local y = Chunk.Height - 1
    		while y > 0 and self:getBlock(x,y,z) == Block.air.id do
    			y = y - 1
    		end
    		self.heightMap[x * 16 + z + 1] = y
    	end
    end
end

-- 计算全部的高度图
function Chunk:updateAllHeightMap()
	self:calcHeightMap(0,0,16,16)
end

-- 更新亮度图
function Chunk:updateVerticalBrightness(x0,z0,x1,z1,initializing)
    local xa0,xa1,za0,za1,ya0,ya1 = x1,x0,z1,z0,Chunk.Height - 1,0
    local BM = Brightness.Max
    local ch = Chunk.Height - 1
    local bs = Block.blocks
    local nb
    for x = x0,x0 + x1 - 1 do
    	for z = z0,z0 + z1 - 1 do
            for y = ch,self:getHeight(x,z),-1 do
                self.brightness[y * 256 + x * 16 + z + 1] = BM
            end
            
            if initializing then
                nb = BM
                for y = self:getHeight(x,z) - 1,0,-1 do
                    nb = nb - bs[self:getBlock(x,y + 1,z)].lightDelay
                    if nb <= 0 then
                        break
                    else
                        self.brightness[y * 256 + x * 16 + z + 1] = nb
                    end
                end
            end
        end
    end
end

function Chunk:updateAllVerticalBrightnessI()
    self:updateVerticalBrightness(0,0,16,16,true)
end

-- 更新全部的亮度图
function Chunk:updateAllVerticalBrightness()
    self:updateVerticalBrightness(0,0,16,16)
end

-- 更新高度图和亮度图
function Chunk:updateMap(x0,z0,x1,z1)
    self:calcHeightMap(x0,z0,x1,z1)
    self:updateVerticalBrightness(x0,z0,x1,z1)
end

-- 更新全部高度图和亮度图
function Chunk:updateAllMap()
    self:updateAllHeightMap()
    self:updateAllVerticalBrightness()
end

function Chunk:setBlock(x,y,z,block)
    self.blocks[y * 256 + x * 16 + z + 1] = block
end

function Chunk:getBlock(x,y,z)
    return self.blocks[y * 256 + x * 16 + z + 1]
end

function Chunk:setBrightness(x,y,z,brightness)
    self.brightness[y * 256 + x * 16 + z + 1] = brightness
end

function Chunk:getBrightness(x,y,z)
    return self.brightness[y * 256 + x * 16 + z + 1]
end

-- 是否为透明方块
function Chunk:isOpacityBlock(x,y,z)
    local id = self.blocks[y * 256 + x * 16 + z + 1]
    return Block.blocks[id].opacityBlock
end

-- 获得高度
function Chunk:getHeight(x,z)
    return self.heightMap[x * 16 + z + 1]
end

function Chunk:getBiome(x,z)
    return self.blockBiome[x * 16 + z + 1]
end
